from django.db import models
from xpinyin import Pinyin

p = Pinyin()


class User(models.Model):
    name = models.CharField(max_length=12, unique=tuple)
    name_py = models.CharField(max_length=6)

    def save(self, *args, **kwargs):
        self.name_py = p.get_initials(self.name, '')
        super(User, self).save(*args, **kwargs)

    # name_py = models.CharField(max_length=6, default=p.get_initials(name.value('',''), ''))

    def __str__(self):
        return self.name


class Record(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    type = models.CharField(max_length=10)
    payed = models.BooleanField()
    money = models.IntegerField()
    date = models.DateField()

    def __str__(self):
        return self.user.name
